{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d8bb3d3e-eec5-4a14-bb36-9fdf6b7d00b2",
   "metadata": {},
   "source": [
    "# Distributed dialogue"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8626bd94-3a0b-4c61-85d6-b157ffc5ac25",
   "metadata": {},
   "source": [
    "This example initializes an assistant agent and a user agent as separate processes and uses RPC to communicate between them. The full codes can be found in in `examples/distributed/distributed_dialog.py`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "605ebd1c-3222-4dce-b974-6377da37d555",
   "metadata": {},
   "source": [
    "To install AgentScope, please follow the steps in [README.md](../README.md#installation)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2417b9fc",
   "metadata": {},
   "source": [
    "First, we need to set the model configs properly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d61bef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_configs = [\n",
    "    {\n",
    "        \"model_type\": \"openai_chat\",\n",
    "        \"config_name\": \"gpt-3.5-turbo\",\n",
    "        \"model_name\": \"gpt-3.5-turbo\",\n",
    "        \"api_key\": \"xxx\",\n",
    "        \"organization\": \"xxx\",\n",
    "        \"generate_args\": {\n",
    "            \"temperature\": 0.0,\n",
    "        },\n",
    "    },\n",
    "    {\n",
    "        \"model_type\": \"openai_chat\",\n",
    "        \"config_name\": \"gpt-4\",\n",
    "        \"model_name\": \"gpt-4\",\n",
    "        \"api_key\": \"xxx\",\n",
    "        \"organization\": \"xxx\",\n",
    "        \"generate_args\": {\n",
    "            \"temperature\": 0.0,\n",
    "        },\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "710f835a-ecc8-481f-a4ab-7f0db33e68f4",
   "metadata": {},
   "source": [
    "Then, we need to initialize two agents: an assistant agent and a user agnent.\n",
    "\n",
    "To facilitate display on jupyter, the agents will be started in a standalone multi-process mode. For a fully distributed version, please refer to `examples/distributed/distributed_dialog.py`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf3226dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import agentscope\n",
    "from agentscope.agents.user_agent import UserAgent\n",
    "from agentscope.agents.dialog_agent import DialogAgent\n",
    "\n",
    "agentscope.init(\n",
    "    model_configs=model_configs\n",
    ")\n",
    "\n",
    "assistant_agent = DialogAgent(\n",
    "    name=\"Assistant\",\n",
    "    sys_prompt=\"You are a helpful assistant.\",\n",
    "    model_config_name=\"gpt-3.5-turbo\",\n",
    "    use_memory=True,\n",
    ").to_dist()\n",
    "user_agent = UserAgent(\n",
    "    name=\"User\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd70c37d",
   "metadata": {},
   "source": [
    "Finally, let's write the main process of the dialogue and chat with the assistant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0f3c851",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from loguru import logger\n",
    "\n",
    "msg = user_agent()\n",
    "while not msg.content.endswith(\"exit\"):\n",
    "    msg = assistant_agent(msg)\n",
    "    logger.chat(msg)\n",
    "    msg = user_agent(msg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
